package com.maomao.maomaochong_i.zhuye;

import android.app.Activity;
import android.os.SystemClock;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.maomao.maomaochong_i.R;
import com.maomao.maomaochong_i.zhuye.Adaper.TuLuenBoAdaper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mrma on 17.5.17.
 */

public class ZhuYeView extends Activity {

    LayoutInflater layoutInflater;
    ViewGroup container;
    private View view;
    ViewPager pager;

    public ZhuYeView(LayoutInflater layoutInflater, ViewGroup container, ViewPager pager){
        this.layoutInflater = layoutInflater;
        this.container = container;
        this.pager = pager;
    }


    public View getview(){
        view = layoutInflater.inflate(R.layout.zhuye,container,false);
        new LuenBo().setTuLuenBo();
        new ShuoYeTV().getShuoYeTVID();
        return view;
    }

    private class ShuoYeTV{
        private TextView SHOYUETV[] = new TextView[7];
        private final int ID[]={0,R.id.ye_1,R.id.ye_2,R.id.ye_3, R.id.ye_4,R.id.ye_5,R.id.ye_6};
        private void getShuoYeTVID(){
            ShuoYeTVshi shi = new ShuoYeTVshi();
            for(int i = 1; i< SHOYUETV.length;i++){
                SHOYUETV[i] = (TextView) view.findViewById(ID[i]);
                SHOYUETV[i].setOnClickListener(shi);
            }

        }

        private class ShuoYeTVshi implements View.OnClickListener{

            @Override
            public void onClick(View v) {
                int yemian = 0;
                switch (v.getId()){
                    case R.id.ye_1 :
                        yemian = 2;
                        break;
                    case R.id.ye_2:
                        yemian = 3;
                        break;
                    case R.id.ye_3:
                        yemian = 4;
                        break;

                }
                pager.setCurrentItem(yemian);

            }
        }//主页面6个按钮的功能
    }



    private class LuenBo{
        private List<ImageView> tuViewList2;
        ViewPager tuPager;
        private int tupian[] = {R.mipmap.ce,R.mipmap.ceshi,R.mipmap.d};

        private Boolean isStop = false;
        private Boolean isDong = true;

        private RadioButton guen[] = new RadioButton[3];
        private final int GUENID[] = {0,R.id.guen1,R.id.guen2,R.id.guen3};
        private void setTuLuenBo() {
            tuPager =(ViewPager) view.findViewById(R.id.tuvp);
            tuViewList2 = tuPianZhiYuan();
            TuLuenBoAdaper tuAdapter =new TuLuenBoAdaper(tuViewList2);
            tuPager.setAdapter(tuAdapter);
            initAction();

        }

        private List<ImageView> tuPianZhiYuan(){
            tuViewList2 =new ArrayList<>();
            for (int aTupian : tupian) {
                ImageView imageView = new ImageView(layoutInflater.getContext());
                imageView.setBackgroundResource(aTupian);
                tuViewList2.add(imageView);
            }
            return tuViewList2;
        }

        private void initAction(){
            BannerLisetener bannerLisetener = new BannerLisetener();
            tuPager.setOnPageChangeListener(bannerLisetener);

            tuPager.setCurrentItem(502);
            tuPager.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            isDong = false;
                            break;
                        case MotionEvent.ACTION_UP:
                            isDong = true;
                            break;
                    }

                    return false;
                }
            });
            luen();
        }

        private class BannerLisetener implements ViewPager.OnPageChangeListener{

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                position = position % tupian.length;
                getGuenId();
                guen[position].setChecked(true);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        }

        private void luen(){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (!isStop){
                        while(isDong){
                            SystemClock.sleep(3000);
                            if(!isDong)
                                continue;
//                                tuPager.setCurrentItem(tuPager.getCurrentItem()+1);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    tuPager.setCurrentItem(tuPager.getCurrentItem()+1);
                                }
                            });
                        }
                        SystemClock.sleep(3000);
                    }            }
            }).start();
        }

        private void getGuenId(){
            for(int i = 0; i < guen.length;i++){
                guen[i] = (RadioButton) view.findViewById(GUENID[i+1]);
            }

        }

    }



}
